<?php

/**
 * method
 *
 * @param string $str
 * @return void
 */
function method($str = 'get')
{
  $str = str_safe($str);
  echo '<input type="hidden" name="_method" value="' . $str . '">';
}
/**
 * get_form_actived
 *
 * @param [type] $val1
 * @param [type] $val2 needle
 * @param string $type ['select','radio','checkbox']
 * @return $res html
 */
function form_input_actived($val1, $val2, $type = 'select')
{
  switch ($type) {
    case 'radio':
      $res = $val1 == $val2 ? 'checked="checked"' : '';
      break;

    case 'checkbox':
      $res = is_in_str($val2, $val1) ? 'checked="checked"' : '';
      break;

    case 'select':
    default:
      $res = $val1 != '' && $val1 == $val2 ? 'selected="selected"' : '';
      break;
  }

  return $res;
}

/**
 * form_prop_create function
 *
 * @param [type] $prop
 * @return $res array
 */
function form_prop_create($prop)
{
  $init = ['method' => 'post', 'class' => 'form'];
  $res = array();

  // 覆盖值
  foreach ($init as $key => $value) {
    $res[$key] = isset($prop[$key]) ? $prop[$key] : $value;
  }

  // 初始值中不存在的值
  foreach ($prop as $key => $value) {
    if (isset($init[$key])) continue;

    $res[$key] = $value;
  }

  return $res;
}

/**
 * list_data_form function
 *
 * @param array $data [['id'=>1,'coupon_name'=>'卡券名称'],...]
 * @param array $format ['value'=>'id','label'=>'coupon_name']
 * @return void
 */
function list_data_form($data = [], $format = [])
{
  $res = array();
  foreach ($data as $key => $value) {
    foreach ($format as $k => $v) {
      $res[$key][$k] = $value[$v];
    }
  }
  return $res;
}
/**
 * row_data_form function
 *
 * @param array $data ['id'=>1,'coupon_name'=>'卡券名称']
 * @param array $format ['id','coupon_name']
 * @return void
 */
function row_data_form($data = [], $format = [])
{
  $res = array();

  foreach ($format as $key => $value) {
    $res[$key]['value'] = is_null($data[$value]) || $data[$value] === '' ? '' : stripslashes($data[$value]);
  }

  return $res;
}

/**
 * form_data_create function
 *
 * @param array $data [['type','label','key','value','class','placeholder','required'],...]
 * @return $res form input
 */
function form_data_create($data, $data_value = [], $part = [])
{
  $res = '';
  $part_count = 0;
  $index = 0;

  foreach ($data as $value) {
    $if_data_value = !empty($data_value) ? true : false;

    switch ($value['type']) {
      case 'part_start':
        $res .= '<div class="tab-panel ' . ($part_count ? '' : 'active') . '" id="tab' . $part_count . '">';
        $part_count++;
        break;
      case 'part_end':
        $res .= '</div>';
        break;
      case 'submit':
        $res .= '<div class="d-grid gap-2"> <button type="' . $value['type'] . '" id="' . $value['key'] . '" class="' . (isset($value['class']) ? $value['class'] : 'btn btn-primary btn-block') . '" ' . iif($value['disabled'], '') . '>' . $value['label'] . '</button> </div>';
        break;
      case 'radio':
        $group = '<div class="form-group"> <label class="form-label">' . $value['label'] . '</label><br> ';
        foreach ($value['value'] as $val) {
          $group .= '<label class="btn btn-outline"> <input class="' . iif($val['class'], 'form-check-input') . '" type="radio" name="' . $value['key'] . '" value="' . $val['value'] . '" ' . (isset($data_value[$index]['value']) ? form_input_actived($data_value[$index]['value'], $val['value'], 'radio') : '') . ' ' . ($val['checked'] ? 'checked="checked"' : '') . '> ' . $val['label'] . ' </label>';
        }
        $group .= ' </div>';
        $res .= $group;
        unset($group);
        $index++;
        break;

      case 'checkbox':
        $group = '<div class="form-group"> <label class="form-label">' . $value['label'] . '</label><br> ';
        foreach ($value['value'] as $val) {
          $group .= '<label class="btn btn-outline"> <input class="' . iif($val['class'], 'form-check-input') . '" type="checkbox" name="' . $value['key'] . '" value="' . $val['value'] . '" ' . (isset($data_value[$index]['value']) ? form_input_actived($data_value[$index]['value'], $val['value'], 'checkbox') : '') . ' ' . ($val['checked'] ? 'checked="checked"' : '') . '> ' . $val['label'] . ' </label>';
        }
        $group .= ' </div>';
        $res .= $group;
        unset($group);
        $index++;
        break;

      case 'select':
        $group = '<div class="form-group"> <label class="form-label" for="' . $value['key'] . '">' . $value['label'] . '</label> <select id="' . $value['key'] . '" name="' . $value['key'] . '" class="' . iif($value['class'], 'form-control form-select') . '" ' . iif($value['required'], '') . ' ' . iif($value['disabled'], '') . '> ';
        if ($value['html']) {
          $group .= $value['value'];
        } else {
          foreach ($value['value'] as $key => $val) {
            $arr = array();
            $arr['value'] = isset($val['value']) ? $val['value'] : $key;
            $arr['label'] = isset($val['label']) ? $val['label'] : $val;
            $group .= '<option value="' . $arr['value'] . '" ' . form_input_actived($data_value[$index]['value'], $arr['value']) .  ' ' . ($val['selected'] ? 'selected="selected"' : '') .  '>' . $arr['label'] . '</option>';
          }
        }
        $group .= ' </select> </div>';
        $res .= $group;
        unset($group);
        $index++;
        break;

      case 'textarea':
        $group = '<div class="form-group"> <label class="form-label" for="' . $value['key'] . '">' . $value['label'] . '</label> ';
        $group .= '<textarea id="' . $value['key'] . '" class="' . iif($value['class'], 'form-control') . '" name="' . $value['key'] . '" ' . iif($value['required'], '') . ' ' . iif($value['disabled'], '') . '>' . (isset($data_value[$index]['value']) ? $data_value[$index]['value'] : @$value['value']) . '</textarea>';
        $group .= ' </div>';
        $res .= $group;
        unset($group);
        $index++;
        break;

      case 'upload_img':
      case 'upload_imgs':
        $group = '<div class="form-group"> <label class="form-label">' . $value['label'] . '</label> ';
        $group .= '<div class="input-group"> <input type="text" name="' . $value['key'] . '" id="' . $value['key'] . '" class="' . iif($value['class'], 'form-control') . '" value="' . (isset($data_value[$index]['value']) ? $data_value[$index]['value'] : @$value['value']) . '" ' . iif($value['required'], '') . ' ' . iif($value['disabled'], '') . '> <button class="btn btn-light btn-outline-secondary" id="' . $value['key'] . '_upload" type="button">选择图片</button> </div>';
        $group .= ' </div>';
        $res .= $group;
        unset($group);
        $index++;
        break;

      case 'upload_attachment':
        $group = '<div class="form-group"> <label class="form-label">' . $value['label'] . '</label> ';
        $group .= '<div class="input-group"> <input name="' . $value['key'] . '" type="text" id="' . $value['key'] . '" class="' . iif($value['class'], 'form-control') . '" value="' . iif($data_value[$index]['value'], @$value['value']) . '" ' . iif($value['required'], '') . ' ' . iif($value['disabled'], '') . '> <button class="btn btn-light btn-outline-secondary" id="' . $value['key'] . '_upload" type="button">选择附件</button> </div>';
        $group .= ' </div>';
        $res .= $group;
        unset($group);
        $index++;
        break;

      case 'editor':
        $group = '<div class="form-group"> <label class="form-label" for="' . $value['key'] . '">' . $value['label'] . '</label> ';
        $group .= '<textarea id="' . $value['key'] . '" type="text" class="' . iif($value['class'], 'editor') . '" name="' . $value['key'] . '" ' . iif($value['required'], '') . ' ' . iif($value['disabled'], '') . '>' . @$data_value[$index]['value'] . '</textarea>';
        $group .= ' </div>';
        $res .= $group;
        unset($group);
        $index++;
        break;

      case 'datetime';
        $group = '<div class="form-group"> <label class="form-label" for="' . $value['key'] . '">' . $value['label'] . '</label> ';
        $group .= '<input type="datetime-local" id="' . $value['key'] . '" name="' . $value['key'] . '" class="' . iif($value['class'], 'form-control') . '" placeholder="' . @$value['placeholder'] . '" value="' . ($if_data_value && !is_numeric($data_value[$index]['value']) ? $data_value[$index]['value'] : $value['value']) . '" ' . iif($value['disabled'], '') . '>';
        $group .= ' </div>';
        $res .= $group;
        unset($group);
        $index++;
        break;

      case 'date':
        $group = '<div class="form-group"> <label class="form-label" for="' . $value['key'] . '">' . $value['label'] . '</label> ';
        $group .= '<input type="date" id="' . $value['key'] . '" name="' . $value['key'] . '" class="' . iif($value['class'], 'form-control') . '" placeholder="' . @$value['placeholder'] . '" value="' . ($if_data_value && !is_numeric($data_value[$index]['value']) ? $data_value[$index]['value'] : $value['value']) . '" ' . iif($value['disabled'], '') . '>';
        $group .= ' </div>';
        $res .= $group;
        unset($group);
        $index++;
        break;

      case 'password':
        $group = '<div class="form-group"> <label class="form-label" for="' . $value['key'] . '">' . $value['label'] . '</label> ';
        $group .= '<input type="password" id="' . $value['key'] . '" name="' . $value['key'] . '" class="' . iif($value['class'], 'form-control') . '" value="' . ($if_data_value ? $data_value[$index]['value'] : $value['value']) . '" placeholder="' . @$value['placeholder'] . '" ' . iif($value['required'], '') . ' ' . iif($value['disabled'], '') . '>';
        $group .= ' </div>';
        $res .= $group;
        unset($group);
        $index++;
        break;

      case 'text':
      default:
        $group = '<div class="form-group"> <label class="form-label" for="' . $value['key'] . '">' . $value['label'] . '</label> ';
        $group .= '<input type="text" id="' . $value['key'] . '" name="' . $value['key'] . '" class="' . iif($value['class'], 'form-control') . '" value="' . ($if_data_value ? $data_value[$index]['value'] : $value['value']) . '" placeholder="' . @$value['placeholder'] . '" ' . iif($value['required'], '') . ' ' . iif($value['disabled'], '') . '>';
        $group .= ' </div>';
        $res .= $group;
        unset($group);
        $index++;
        break;
    }
  }

  return $res;
}

/**
 * form_create function
 *
 * @param array $form_prop form porps
 * @return $res form
 */
function form_create($form_prop = [], $input_data = [], $part = [], $input_value = [])
{
  $form_str = '';
  $input_value_filter = array();
  $form_prop = form_prop_create($form_prop);
  foreach ($form_prop as $key => $value) {
    $form_str .= ' ' . $key . '="' . $value . '"';
  }

  $res = '<div class="card"> <div class="card-header">';
  $res .= ' <ul class="nav nav-tabs card-header-tabs">';
  foreach ($part as $key => $val) {
    $res .= '<li class="nav-item" data-id="tab' . $key . '"> <a class="nav-link ' . ($key ? '' : 'active') . '" href="">' . $val . '</a> </li>';
  }
  $res .= '</ul>';
  $res .= '</div> <div class="card-body">';
  $res .= '<div class="tab-content">';
  $res .= '<form' . $form_str . '>';
  unset($form_str); // 清理

  // 组织row_data_form数据
  if (!empty($input_value)) {
    $filter = array();
    foreach ($input_data as $value) {
      if ($value['key'] == 'save') continue;
      // 密码和密码效验默认为空
      if (is_null($value['key']) || empty($value['key'])) continue;
      $filter[] = strpos($value['key'], '_psw') !== false || strpos($value['key'], '_password') !== false ? '' : $value['key'];
    }

    $input_value_filter = row_data_form($input_value, $filter);
  }

  $res .= form_data_create($input_data, $input_value_filter, $part);
  $res .= '</div>';
  $res .= '</div> </form> </div>';

  return $res;
}
